// pages/comfirmPay/comfirmPay.js
var WxParse = require('../../wxParse/wxParse.js');
let app=getApp();
let jumpurl=app.jumpurl.url; 
let orderid;
let ydHidden=true;
let kaiqiflag; //true代表开通优豆 
let weixinflag=true; //true代表微信支付  false代表余额支付
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money:500.00,
    wxHidden:true,
    pay_type:0,
    yd_type:1,//0代表不抵扣 1代表抵扣
    yue:20.00,
    youdou:100,
    ydHidden:true,
    gzHidden:true,
    weixinflag:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    orderid=options.orderid;
    wx.request({
      url: jumpurl+'pay/gopay',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      data:{
        openid:app.userList.openid,
        orderid: options.orderid,
      },
      success:function(res){
        
          that.setData({
            price:res.data.msg.message.price,
            yue:res.data.msg.message1.money

          })
          
      },
      fail:function(err){
        console.log(err);
      }

    })
    //优豆
    wx.request({
     
      url: jumpurl+'youdou/index',
      data:{
        shopid:app.userList.shopid,
        openid:app.userList.openid
      },
      success:function(res){
        console.log("success");
        console.log(res);
        var kaiqi=res.data.kaiqi;
        if(kaiqi==1){
          kaiqiflag=true;
        }else{
          kaiqiflag=false;
        }
        that.setData({
          kaiqiflag:kaiqiflag
        })
        console.log(kaiqiflag);
        console.log(weixinflag)
        that.setData({
          youdou:res.data.youdou,
          keyong:res.data.youdou,
          dikou:res.data.dikou
        })
      },
      fail:function(err){
        console.log("err"+err)
      }

      
    })
  },
  comfirmPay:function(e){
   console.log(e);
    console.log(this.data.pay_type);
    var that=this;
    wx.request({
      url: jumpurl + 'pay/index',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: {
        tag: that.data.pay_type,
        openid: app.userList.openid,
        orderid: orderid,
        shopid: app.userList.shopid,
        yd_type: e.currentTarget.dataset.type,
      },
      success: function (res) {
        console.log(res);
        console.log('-----------------------');

        if (res.data.state == 1) {
          that.setData({
            order_no: res.data.order_no,
          })
          // --------- 订单生成成功，发起支付请求 ------------------
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,   //字符串随机数
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            'success': function (res) {
              //requestPayment:ok==>调用支付成功
              if (res.errMsg == "requestPayment:ok") {
                wx.request({
                  url: jumpurl + 'pay/notify',
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    yd_type: e.currentTarget.dataset.type,
                    order_no: that.data.order_no,
                  },
                  success: function (res) {
                    wx.showToast({
                      title: '下单成功',
                      icon: 'none',
                      image: '',
                      duration: 2000,
                      mask: true,
                      success: function (res) {
                        setTimeout(function () {
                          that.onLoad();
                        }, 2000);
                        setTimeout(function () {
                          wx.redirectTo({
                            url: '/pages/mine/mine',
                          })
                        }, 1000)
                      },
                    })
                  }
                })
              }
            },
            'fail': function (res) {
              console.log(res);
            },
            'complete': function (res) {
              console.log(res);
            }
          })
        } else if (res.data.state == 0) {
          wx.showToast({
            title: res.data.Msg,
            icon: 'fail',
            duration: 1000
          })
        } else {
          wx.showToast({
            title: '付款成功',
            icon: 'fail',
            duration: 1000
          })
          setTimeout(function(){
            wx.redirectTo({
              url: '/pages/mine/mine',
            })
          },1000)
         
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  //弹出计价规则
  guize:function(e){
    // 定义变量
    var that=this;
    var article ="传值的内容传值的内容";
    //显示弹出面板
    that.setData({
       gzHidden:false
    })
    WxParse.wxParse('article', 'html', article, that, 5);
  },
//关闭计价规则
shutMenu:function(e){
  var that=this;
  that.setData({
    gzHidden: true
  })
},
//支付方式
  paymenttype:function(e){
    var num = e.currentTarget.dataset.num;
    console.log(num);
    if (num == 0){//微信支付
      weixinflag=true;
      this.setData({
        wxHidden:true,
        pay_type: num,
        weixinflag:true,
       
      })
    }else{
      weixinflag=false;
      this.setData({
        wxHidden: false,
        pay_type: num,
        weixinflag:false
      })
    }

    
  },

  //优豆抵扣
  youdoudikou:function(e){
    // var dikouflag=e.currentTarget.dataset.dikou;
    console.log(e);
    if (ydHidden){
      ydHidden=false;
      this.setData({
        ydHidden:false,
        yd_type:0,
       
      })
    }else{
      ydHidden=true;
      this.setData({
        ydHidden:true,
        yd_type:1,
        
      })
    }
  }
 

 

 

})